﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ObjectManager;

namespace QuanLyDiem
{
    public partial class frmLogin : Form
    {
        private static frmLogin frm = null;

        public event Login LoginSuccess = null;

        private frmLogin()
        {
            InitializeComponent();
            //cnn = Connection.Connect();
        }

        public static frmLogin Create()
        {
            if (frm == null)
            {
                frm = new frmLogin();
                return frm;
            }
            return frm;
        }

        private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (frm != null)
            {
                frm.Dispose();
                frm = null;
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            DangNhap login = new DangNhap();
            login.ThongTin.TenDangNhap = txtUser.Text.Trim().ToLower();
            login.ThongTin.MatKhau = txtPassword.Text;
            if (login.Check())
            {
                //MessageBox.Show("OK");
                if (LoginSuccess != null)
                {
                    InfoLogin info = new InfoLogin();
                    info.Login = login;
                    LoginSuccess(this, info);
                }
                this.Close();
            }
            else
            {
                MessageBox.Show("Tên đăng nhập hoặc mật khẩu không chính xác", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtUser.Focus();
                txtUser.SelectAll();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtUser_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnLogin.PerformClick();
            }
        }

        private void txtUser_Enter(object sender, EventArgs e)
        {
            txtUser.SelectAll();
        }

        private void txtPassword_Enter(object sender, EventArgs e)
        {
            txtPassword.SelectAll();
        }
    }
}
